//
//  CarConditionHeadView.m
//  carcareIOS
//
//  Created by 王然 on 16/8/17.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "CarConditionHeadView.h"
typedef NS_ENUM(NSInteger , View_Type) {
    //    ImageV_Head_Voltage = 50,//头部的 当前电压前面的叹号图片
    ImageV_Head_Corner = 150,//头部小圆点 view
    Label_Head_Num = 100,//头部数值标题tag
    Label_Head_Unit = 80,//头部的单位tag
};

@interface CarConditionHeadView()

@property (nonatomic , copy) void(^btnClick)(NSInteger tagIndex);

@end
@implementation CarConditionHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[self resetBackgroundColor:[UIColor whiteColor]] resetBorderWidth:0.5 borderColor:COLOR_HEXSTRING(@"E2E2DD")];
        self.userInteractionEnabled = YES;
        
        self.layer.shadowColor = COLOR_HEXSTRING(@"#000000").CGColor;
        self.layer.shadowRadius = 2.5;
        self.layer.shadowOffset = CGSizeMake( 0, 2.5);
        self.layer.shadowOpacity = 0.08;
        self.layer.cornerRadius = 2;
    }
    return self;
}
- (void)reloaNameTitles:(NSArray *)nameTitles units:(NSArray *)unites click:(void(^)(NSInteger tagIndex))click
{
    [self removeAllSubviews];
    
    if (click) self.btnClick = click;
    for (int index = 0; index < nameTitles.count; index ++) {
        CGFloat headSmallViewW = self.width / 3;
        CGFloat headSmallViewH = self.height;
        CGFloat headSmallViewY = 0;
        UIButton * headFuncButton = [self headSmallView:@"" unitLabelStr:unites[index] nameLabelStr:nameTitles[index] withImageColor:[Theme mainNavColor] withViewFrame:CGRectMake(0 + index * headSmallViewW, headSmallViewY, headSmallViewW,headSmallViewH) withViewOnView:self indexNum:index ];
        [headFuncButton addTarget:self action:@selector(headFuncButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}
- (void)headFuncButtonClick:(UIButton *)sender
{
    if (self.btnClick) self.btnClick(sender.tag);
}
- (UILabel *)getNumLabelWithIndex:(NSInteger)index
{
    return (UILabel *)[self viewWithTag:index + Label_Head_Num];
}
- (UIImageView *)getCornerImageVWithIndex:(NSInteger)index
{
    return (UIImageView *)[self viewWithTag:index + ImageV_Head_Corner];
}
- (UIButton *)headSmallView:(NSString *)labelTitleStr unitLabelStr:(NSString *)unitStr  nameLabelStr:(NSString *)nameStr withImageColor:(UIColor *)imageColor withViewFrame:(CGRect)rect withViewOnView:(UIView *)view indexNum:(int)index
{
    UIView * backView = [[[UIView viewWithFrame:rect] resetBackgroundColor:[UIColor clearColor]] setupOnView:view];
    backView.userInteractionEnabled = YES;
    CGFloat titleLabelCenterY = rect.size.height / 2 + 10;
    
    //小圆点
    UIImageView * cornerImageV = [Factory imageViewWithFrame:CGRectMake(0,0, 6, 6) image:nil onView:backView];
    cornerImageV.layer.cornerRadius = cornerImageV.frame.size.width / 2;
    cornerImageV.layer.masksToBounds = YES;
    cornerImageV.backgroundColor = imageColor;
    cornerImageV.tag = ImageV_Head_Corner + index;
    
    //对于当前电压添加的叹号图片
    //    UIImageView * voiltageImageV = [Factory imageViewWithCenter:CGPointZero image:ImageNamed(@"voiltageBangImage") onView:backView];
    //    voiltageImageV.tag = ImageV_Head_Voltage + index;
    //    voiltageImageV.hidden = YES;
    
    //单位
    UILabel * unitLabel = [Factory flabelWithCenter:CGPointZero font:FONT_DEFAULT_Light(11 * __MAIN_RATIO_375) text:unitStr textColor:COLOR_HEXSTRING(@"#717171") onView:backView resetSizeFinish:^(UILabel *label) {
        [unitLabel setSize:CGSizeMake(label.frame.size.width, 12)];
    }];
    unitLabel.tag = Label_Head_Unit + index;
    unitLabel.textAlignment = NSTextAlignmentLeft;
    
    //名称
    CGFloat nameLableCenterY = rect.size.height / 2 - 10;
    UILabel * nameLabel = [Factory flabelWithCenter:CGPointMake(rect.size.width / 2 + 5, nameLableCenterY) font:FONT_DEFAULT_Light(12 * __MAIN_RATIO_375) text:nameStr textColor:COLOR_HEXSTRING(@"#717171") onView:backView resetSizeFinish:^(UILabel *label) {
        [nameLabel setSize:label.size];
        [cornerImageV setCenter:CGPointMake(label.frame.origin.x / 3 * 2 ,nameLableCenterY)];
        //        [voiltageImageV setCenter:CGPointMake(label.frame.origin.x / 3 * 2 ,nameLableCenterY)];
    }];
    
    //数量title
    UILabel * titleLabel = [Factory flabelWithCenter:CGPointMake(rect.size.width / 2 , titleLabelCenterY) font:FONT_DEFAULT_Light((__MAIN_WIDTH == 320 ? 13 : 14)) text:labelTitleStr textColor:[Theme wordColor] onView:backView resetSizeFinish:^(UILabel *label) {
        [titleLabel setSize:label.size];
        [unitLabel setFrame:CGRectMake(label.frame.origin.x + label.frame.size.width + 2, label.frame.origin.y + 4 * __MAIN_RATIO_375, unitLabel.frame.size.width, 12)];
        
    }];
    titleLabel.tag = Label_Head_Num + index;
    
    UIButton * backButton = [[UIButton buttonWithFrame:backView.bounds title:@"" textColor:[UIColor clearColor] font:FONT_BUTTON click:nil] setupOnView:backView];
    
    backButton.tag = 120 + index;
    
    return backButton;
}

@end
